<template>
  <transition name="slide-up">
    <div class="setting-wrapper" v-show="menuVisible && settingVisible === 2">
      <div class="setting-progress">
        <div class="read-time-wrapper">
          <span class="read-time-text">{{getReadTimeText()}}</span>
          <span class="icon-forward"></span>
        </div>
        <div class="progress-wrapper">
          <div class="progress-icon-wrapper" @click="prevSection()">
            <span class="icon-back"></span>
          </div>
          <input
            class="progress"
            type="range"
            max="100"
            min="0"
            step="1"
            @change="onProgressChange($event.target.value)"
            @input="onProgressInput($event.target.value)"
            :value="progress"
            :disabled="!bookAvailable"
            ref="progress"
          />
          <div class="progress-icon-wrapper" @click="nextSection()">
            <span class="icon-forward"></span>
          </div>
        </div>
        <div class="text-wrapper">
          <span class="progress-section-text">{{getSectionName}}</span>
          <span>({{bookAvailable ? progress + '%' : '加载中...'}})</span>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { ebookMixin } from "../../utils/mixin";

export default {
  mixins: [ebookMixin],
  data() {
    return {};
  },
  computed: {
    getSectionName() {
      // 获取章节的名称
      // if (this.section) {
      //   const sectionInfo = this.currentBook.section(this.section);
      //   if (
      //     sectionInfo &&
      //     sectionInfo.href &&
      //     this.currentBook &&
      //     this.currentBook.navigation
      //   ) {
      //     return this.currentBook.navigation.get(sectionInfo.href).label;
      //   }
      // }
      // return "";
      return this.section ? this.navigation[this.section].label : ''
    }
  },
  methods: {
    onProgressChange(progress) {
      // 点击进度条后 显示对应的内容
      this.setProgress(progress).then(() => {
        this.displayProgress();
        this.updateProgressBg();
      });
    },
    onProgressInput(progress) {
      // 拖动进度条后 显示对应的内容
      this.setProgress(progress).then(() => {
        this.updateProgressBg();
      });
    },
    displayProgress() {
      // 显示进度 百分比
      const cfi = this.currentBook.locations.cfiFromPercentage(
        this.progress / 100
      );
      // console.log(cfi)
      this.display(cfi); // 调用自定义的渲染方法
      // this.currentBook.rendition.display(cfi).then(()=>{// 设置百分比
      //   this.refreshLocation() //然后 保存阅读进度
      // })
    },
    updateProgressBg() {
      // 进度条背景色
      // console.log(this.progress);
      // .cssText = "background-color:red !important";
      //   this.$refs.progress.style.backgroundSize = `${this.progress}% 100%`;
      this.$refs.progress.style.cssText = `background-size:${this.progress}% 100% !important`;
    },
    prevSection() {
      // 上一章
      if (this.section > 0 && this.bookAvailable) {
        this.setSection(this.section - 1).then(() => {
          this.displaySection();
        });
      }
    },
    nextSection() {
      // 下一章
      // this.currentBook.spine.length 总页数
      if (
        this.section < this.currentBook.spine.length - 1 &&
        this.bookAvailable
      ) {
        this.setSection(this.section + 1).then(() => {
          this.displaySection();
        });
      }
    },
    displaySection() {
      const sectionInfo = this.currentBook.section(this.section);
      if (sectionInfo && sectionInfo.href) {
        // this.currentBook.rendition.display(sectionInfo.href).then(() => {
        //   this.refreshLocation()
        // })
        this.display(sectionInfo.href); // 调用自定义的渲染方法
      }
    }
  },
  updated() {
    this.updateProgressBg();
  }
};
</script>

<style lang='scss' scoped>
@import "../../assets/styles/global.scss";
.setting-wrapper {
  position: absolute;
  bottom: px2rem(48);
  left: 0;
  z-index: 101;
  width: 100%;
  height: px2rem(90);
  background: white;
  box-shadow: 0 px2rem(-8) px2rem(8) rgba(0, 0, 0, 0.15);
  .setting-progress {
    position: relative;
    width: 100%;
    height: 100%;
    .read-time-wrapper {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: px2rem(40);
      @include center;
      font-size: px2rem(12);
    }
    .progress-wrapper {
      width: 100%;
      height: 100%;
      @include center;
      padding: 0 px2rem(15);
      box-sizing: border-box;
      .progress-icon-wrapper {
        font-size: px2rem(20);
      }
      .progress {
        width: 100%;
        -webkit-appearance: none;
        height: px2rem(2);
        //background: -webkit-linear-gradient(#999, #999) no-repeat, #ddd;
        //background-size: 0 100% !important;
        margin: 0 px2rem(10);
        &:focus {
          outline: none;
        }
        &::-webkit-slider-thumb {
          -webkit-appearance: none;
          height: px2rem(20);
          width: px2rem(20);
          border-radius: 50%;
          background: white;
          box-shadow: 0 4px 4px 0 rgba(0, 0, 0, 0.15);
          border: px2rem(1) solid #ddd;
        }
      }
    }
    .text-wrapper {
      position: absolute;
      left: 0;
      bottom: px2rem(10);
      width: 100%;
      color: #333;
      font-size: px2rem(12);
      padding: 0 px2rem(15);
      box-sizing: border-box;
      // text-align: center;
      @include center;
      .progress-section-text {
        @include ellipsis;
      }
    }
  }
}
</style>
